/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.refunite.mobile.s60.ui;

import org.refunite.mobile.s60.RefugeesUnited;
import org.refunite.mobile.s60.dto.ServerConfigData;
import org.refunite.mobile.s60.exception.TechnicalException;
import org.refunite.mobile.s60.persistence.PersistenceStoreProvider;
import org.refunite.mobile.s60.ws.WebServiceProvider;
import org.refunite.mobile.s60.util.FormController;
import java.io.ByteArrayInputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.javarosa.core.model.FormDef;
import org.javarosa.xform.util.XFormUtils;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author I040022
 */
public class SettingsScreen implements CommandListener, ItemCommandListener {

    private RefugeesUnited midlet;

    private Alert configSaved = new Alert("Info", "Server configuration saved", null, AlertType.INFO);
    private Alert couldNotUpdateAlert = new Alert("Could not update", "Please check Server Settings.", null, AlertType.ERROR);
    private Alert formUpdatedAlert = new Alert("Info", "Form updated", null, AlertType.INFO);

    private ChoiceGroup choiceGroup;
    private boolean languageSelectorScreen = true;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private java.util.Hashtable __previousDisplayables = new java.util.Hashtable();
    private Form regFormSettingsForm;
    private StringItem stringItem1;
    private StringItem languageItem;
    private Form serverSettingsForm;
    private TextField inputPassword;
    private TextField inputUsername;
    private TextField inputServerURL;
    private List list;
    private Form selectLanguage;
    private WaitScreen updateForm;
    private Command backFromRegFormSettings;
    private Command okAtRegFormSettings;
    private Command okAtServerSettings;
    private Command backFromServerSettings;
    private Command backCommand;
    private Command updateCommand;
    private Command backCommand1;
    private Command languageCommand;
    private Command okCommand;
    private Image imageServer;
    private SimpleCancellableTask task;
    private Image image;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The SettingsScreen constructor.
     * @param midlet the midlet used for getting
     */
    public SettingsScreen (MIDlet midlet) {
        this.midlet = (RefugeesUnited)midlet;
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    /**
     * Switches a display to previous displayable of the current displayable.
     * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
     */
    private void switchToPreviousDisplayable() {
        Displayable __currentDisplayable = getDisplay().getCurrent();
        if (__currentDisplayable != null) {
            Displayable __nextDisplayable = (Displayable) __previousDisplayables.get(__currentDisplayable);
            if (__nextDisplayable != null) {
                switchDisplayable(null, __nextDisplayable);
            }
        }
    }
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|2-switchDisplayable|0|2-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|2-switchDisplayable|0|2-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|2-switchDisplayable|1|2-postSwitch
        Displayable __currentDisplayable = display.getCurrent();
        if (__currentDisplayable != null  &&  nextDisplayable != null) {
            __previousDisplayables.put(nextDisplayable, __currentDisplayable);
        }
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|2-switchDisplayable|1|2-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|2-switchDisplayable|2|
    //</editor-fold>//GEN-END:|2-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: display ">//GEN-BEGIN:|17-entry|0|18-preAction
    /**
     * Performs an action assigned to the display entry-point.
     */
    public void display() {//GEN-END:|17-entry|0|18-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|17-entry|1|18-postAction
        // write post-action user code here
        loadConfiguration();
    }//GEN-BEGIN:|17-entry|2|
    //</editor-fold>//GEN-END:|17-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|4-commandAction|0|4-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|4-commandAction|0|4-preCommandAction
        // write pre-action user code here
        if (displayable == list) {//GEN-BEGIN:|4-commandAction|1|78-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|4-commandAction|1|78-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|4-commandAction|2|78-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|4-commandAction|3|86-preAction
                // write pre-action user code here
                this.midlet.backToMenu();//GEN-LINE:|4-commandAction|4|86-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|5|29-preAction
        } else if (displayable == regFormSettingsForm) {
            if (command == backFromRegFormSettings) {//GEN-END:|4-commandAction|5|29-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|4-commandAction|6|29-postAction
                // write post-action user code here
            } else if (command == okAtRegFormSettings) {//GEN-LINE:|4-commandAction|7|31-preAction
                // write pre-action user code here
//GEN-LINE:|4-commandAction|8|31-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|9|104-preAction
        } else if (displayable == selectLanguage) {
            if (command == backCommand1) {//GEN-END:|4-commandAction|9|104-preAction
                // write pre-action user code here
                if(this.languageSelectorScreen){
                    switchToPreviousDisplayable();//GEN-LINE:|4-commandAction|10|104-postAction
                // write post-action user code here
                }else{
                    this.midlet.backToMenu();
                }
            } else if (command == okCommand) {//GEN-LINE:|4-commandAction|11|107-preAction
                // write pre-action user code here
                this.saveFormLanguage();//GEN-LINE:|4-commandAction|12|107-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|13|34-preAction
        } else if (displayable == serverSettingsForm) {
            if (command == backFromServerSettings) {//GEN-END:|4-commandAction|13|34-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|4-commandAction|14|34-postAction
                // write post-action user code here
            } else if (command == okAtServerSettings) {//GEN-LINE:|4-commandAction|15|36-preAction
                // write pre-action user code here
                this.saveConfiguration();//GEN-LINE:|4-commandAction|16|36-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|17|118-preAction
        } else if (displayable == updateForm) {
            if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|4-commandAction|17|118-preAction
                // write pre-action user code here
//GEN-LINE:|4-commandAction|18|118-postAction
                // write post-action user code here
            } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|4-commandAction|19|117-preAction
                // write pre-action user code here
//GEN-LINE:|4-commandAction|20|117-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|21|4-postCommandAction
        }//GEN-END:|4-commandAction|21|4-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|4-commandAction|22|
    //</editor-fold>//GEN-END:|4-commandAction|22|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverSettingsForm ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of serverSettingsForm component.
     * @return the initialized component instance
     */
    public Form getServerSettingsForm() {
        if (serverSettingsForm == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            serverSettingsForm = new Form("Server Settings", new Item[] { getInputServerURL(), getInputUsername(), getInputPassword() });//GEN-BEGIN:|26-getter|1|26-postInit
            serverSettingsForm.addCommand(getBackFromServerSettings());
            serverSettingsForm.addCommand(getOkAtServerSettings());
            serverSettingsForm.setCommandListener(this);//GEN-END:|26-getter|1|26-postInit
            // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return serverSettingsForm;
    }
    //</editor-fold>//GEN-END:|26-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: regFormSettingsForm ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of regFormSettingsForm component.
     * @return the initialized component instance
     */
    public Form getRegFormSettingsForm() {
        if (regFormSettingsForm == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            regFormSettingsForm = new Form("Registration Form Settings", new Item[] { getLanguageItem(), getStringItem1() });//GEN-BEGIN:|27-getter|1|27-postInit
            regFormSettingsForm.addCommand(getBackFromRegFormSettings());
            regFormSettingsForm.addCommand(getOkAtRegFormSettings());
            regFormSettingsForm.setCommandListener(this);//GEN-END:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return regFormSettingsForm;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backFromRegFormSettings ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of backFromRegFormSettings component.
     * @return the initialized component instance
     */
    public Command getBackFromRegFormSettings() {
        if (backFromRegFormSettings == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            backFromRegFormSettings = new Command("Back", Command.BACK, 0);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return backFromRegFormSettings;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okAtRegFormSettings ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of okAtRegFormSettings component.
     * @return the initialized component instance
     */
    public Command getOkAtRegFormSettings() {
        if (okAtRegFormSettings == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            okAtRegFormSettings = new Command("Ok", Command.OK, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return okAtRegFormSettings;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backFromServerSettings ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of backFromServerSettings component.
     * @return the initialized component instance
     */
    public Command getBackFromServerSettings() {
        if (backFromServerSettings == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            backFromServerSettings = new Command("Back", Command.BACK, 0);//GEN-LINE:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return backFromServerSettings;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okAtServerSettings ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of okAtServerSettings component.
     * @return the initialized component instance
     */
    public Command getOkAtServerSettings() {
        if (okAtServerSettings == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            okAtServerSettings = new Command("Save", Command.OK, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return okAtServerSettings;
    }
    //</editor-fold>//GEN-END:|35-getter|2|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: inputServerURL ">//GEN-BEGIN:|60-getter|0|60-preInit
    /**
     * Returns an initiliazed instance of inputServerURL component.
     * @return the initialized component instance
     */
    public TextField getInputServerURL() {
        if (inputServerURL == null) {//GEN-END:|60-getter|0|60-preInit
            // write pre-init user code here
            inputServerURL = new TextField("Server URL:", null, 64, TextField.URL);//GEN-BEGIN:|60-getter|1|60-postInit
            inputServerURL.setLayout(ImageItem.LAYOUT_LEFT | Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | Item.LAYOUT_EXPAND | Item.LAYOUT_2);//GEN-END:|60-getter|1|60-postInit
            // write post-init user code here
        }//GEN-BEGIN:|60-getter|2|
        return inputServerURL;
    }
    //</editor-fold>//GEN-END:|60-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: inputUsername ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of inputUsername component.
     * @return the initialized component instance
     */
    public TextField getInputUsername() {
        if (inputUsername == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            inputUsername = new TextField("Username:", null, 32, TextField.ANY);//GEN-BEGIN:|61-getter|1|61-postInit
            inputUsername.setLayout(ImageItem.LAYOUT_LEFT | Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | Item.LAYOUT_EXPAND | Item.LAYOUT_2);//GEN-END:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return inputUsername;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: inputPassword ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of inputPassword component.
     * @return the initialized component instance
     */
    public TextField getInputPassword() {
        if (inputPassword == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            inputPassword = new TextField("Password", null, 32, TextField.ANY | TextField.PASSWORD);//GEN-BEGIN:|62-getter|1|62-postInit
            inputPassword.setLayout(ImageItem.LAYOUT_LEFT | Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | Item.LAYOUT_EXPAND | Item.LAYOUT_2);//GEN-END:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return inputPassword;
    }
    //</editor-fold>//GEN-END:|62-getter|2|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|76-getter|0|76-preInit
    /**
     * Returns an initiliazed instance of list component.
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|76-getter|0|76-preInit
            // write pre-init user code here
            list = new List("Settings", Choice.IMPLICIT);//GEN-BEGIN:|76-getter|1|76-postInit
            list.append("Server Settings", getImageServer());
            list.append("Form Settings", getImage());
            list.addCommand(getBackCommand());
            list.setCommandListener(this);
            list.setFitPolicy(Choice.TEXT_WRAP_OFF);
            list.setSelectedFlags(new boolean[] { false, false });//GEN-END:|76-getter|1|76-postInit
            // write post-init user code here
        }//GEN-BEGIN:|76-getter|2|
        return list;
    }
    //</editor-fold>//GEN-END:|76-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|76-action|0|76-preAction
    /**
     * Performs an action assigned to the selected list element in the list component.
     */
    public void listAction() {//GEN-END:|76-action|0|76-preAction
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|76-action|1|80-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Server Settings")) {//GEN-END:|76-action|1|80-preAction
                // write pre-action user code here
                switchDisplayable(null, getServerSettingsForm());//GEN-LINE:|76-action|2|80-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Form Settings")) {//GEN-LINE:|76-action|3|82-preAction
                // write pre-action user code here
                this.languageSelectorScreen = true;
                switchDisplayable(null, getRegFormSettingsForm());//GEN-LINE:|76-action|4|82-postAction
                // write post-action user code here
            }//GEN-BEGIN:|76-action|5|76-postAction
        }//GEN-END:|76-action|5|76-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|76-action|6|
    //</editor-fold>//GEN-END:|76-action|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|85-getter|0|85-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|85-getter|0|85-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|85-getter|1|85-postInit
            // write post-init user code here
        }//GEN-BEGIN:|85-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|85-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageServer ">//GEN-BEGIN:|90-getter|0|90-preInit
    /**
     * Returns an initiliazed instance of imageServer component.
     * @return the initialized component instance
     */
    public Image getImageServer() {
        if (imageServer == null) {//GEN-END:|90-getter|0|90-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|90-getter|1|90-@java.io.IOException
                imageServer = Image.createImage("/serverSettings.png");
            } catch (java.io.IOException e) {//GEN-END:|90-getter|1|90-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|90-getter|2|90-postInit
            // write post-init user code here
        }//GEN-BEGIN:|90-getter|3|
        return imageServer;
    }
    //</editor-fold>//GEN-END:|90-getter|3|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|93-getter|0|93-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|93-getter|0|93-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("", "Update Form", Item.BUTTON);//GEN-BEGIN:|93-getter|1|93-postInit
            stringItem1.addCommand(getUpdateCommand());
            stringItem1.setItemCommandListener(this);
            stringItem1.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | Item.LAYOUT_2);//GEN-END:|93-getter|1|93-postInit
            // write post-init user code here
        }//GEN-BEGIN:|93-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|93-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Items ">//GEN-BEGIN:|5-itemCommandAction|0|5-preItemCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular item.
     * @param command the Command that was invoked
     * @param displayable the Item where the command was invoked
     */
    public void commandAction(Command command, Item item) {//GEN-END:|5-itemCommandAction|0|5-preItemCommandAction
        // write pre-action user code here
        if (item == languageItem) {//GEN-BEGIN:|5-itemCommandAction|1|109-preAction
            if (command == languageCommand) {//GEN-END:|5-itemCommandAction|1|109-preAction
                // write pre-action user code here
                selectLanguage = null;
                switchDisplayable(null, getSelectLanguage());//GEN-LINE:|5-itemCommandAction|2|109-postAction
                // write post-action user code here
            }//GEN-BEGIN:|5-itemCommandAction|3|95-preAction
        } else if (item == stringItem1) {
            if (command == updateCommand) {//GEN-END:|5-itemCommandAction|3|95-preAction
                // write pre-action user code here
                switchDisplayable(null, getUpdateForm());//GEN-LINE:|5-itemCommandAction|4|95-postAction
                // write post-action user code here
            }//GEN-BEGIN:|5-itemCommandAction|5|5-postItemCommandAction
        }//GEN-END:|5-itemCommandAction|5|5-postItemCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|5-itemCommandAction|6|
    //</editor-fold>//GEN-END:|5-itemCommandAction|6|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: updateCommand ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of updateCommand component.
     * @return the initialized component instance
     */
    public Command getUpdateCommand() {
        if (updateCommand == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            updateCommand = new Command("Update", Command.ITEM, 0);//GEN-LINE:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return updateCommand;
    }
    //</editor-fold>//GEN-END:|94-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: languageCommand ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of languageCommand component.
     * @return the initialized component instance
     */
    public Command getLanguageCommand() {
        if (languageCommand == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            languageCommand = new Command("", Command.ITEM, 0);//GEN-LINE:|99-getter|1|99-postInit
            // write post-init user code here
        }//GEN-BEGIN:|99-getter|2|
        return languageCommand;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: selectLanguage ">//GEN-BEGIN:|101-getter|0|101-preInit
    /**
     * Returns an initiliazed instance of selectLanguage component.
     * @return the initialized component instance
     */
    public Form getSelectLanguage() {
        if (selectLanguage == null) {//GEN-END:|101-getter|0|101-preInit
            // write pre-init user code here
            selectLanguage = new Form("Select Language", new Item[] { });//GEN-BEGIN:|101-getter|1|101-postInit
            selectLanguage.addCommand(getBackCommand1());
            selectLanguage.addCommand(getOkCommand());
            selectLanguage.setCommandListener(this);//GEN-END:|101-getter|1|101-postInit
            // write post-init user code here
            addAvailableLanguages();

        }//GEN-BEGIN:|101-getter|2|
        return selectLanguage;
    }
    //</editor-fold>//GEN-END:|101-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|103-getter|0|103-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|103-getter|0|103-preInit
            // write pre-init user code here
            backCommand1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|103-getter|1|103-postInit
            // write post-init user code here
        }//GEN-BEGIN:|103-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|103-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|106-getter|0|106-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|106-getter|0|106-preInit
            // write pre-init user code here
            okCommand = new Command("Select", Command.OK, 0);//GEN-LINE:|106-getter|1|106-postInit
            // write post-init user code here
        }//GEN-BEGIN:|106-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|106-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: languageItem ">//GEN-BEGIN:|108-getter|0|108-preInit
    /**
     * Returns an initiliazed instance of languageItem component.
     * @return the initialized component instance
     */
    public StringItem getLanguageItem() {
        if (languageItem == null) {//GEN-END:|108-getter|0|108-preInit
            // write pre-init user code here
            languageItem = new StringItem("Form language", null);//GEN-BEGIN:|108-getter|1|108-postInit
            languageItem.addCommand(getLanguageCommand());
            languageItem.setItemCommandListener(this);
            languageItem.setDefaultCommand(getLanguageCommand());
            languageItem.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_AFTER | Item.LAYOUT_EXPAND | Item.LAYOUT_2);//GEN-END:|108-getter|1|108-postInit
            // write post-init user code here
            String savedLanguage = PersistenceStoreProvider.getFormLanguage();
            if(savedLanguage != null){
                languageItem.setText(savedLanguage);
            }
        }//GEN-BEGIN:|108-getter|2|
        return languageItem;
    }
    //</editor-fold>//GEN-END:|108-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image ">//GEN-BEGIN:|113-getter|0|113-preInit
    /**
     * Returns an initiliazed instance of image component.
     * @return the initialized component instance
     */
    public Image getImage() {
        if (image == null) {//GEN-END:|113-getter|0|113-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|113-getter|1|113-@java.io.IOException
                image = Image.createImage("/formSettings.png");
            } catch (java.io.IOException e) {//GEN-END:|113-getter|1|113-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|113-getter|2|113-postInit
            // write post-init user code here
        }//GEN-BEGIN:|113-getter|3|
        return image;
    }
    //</editor-fold>//GEN-END:|113-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: updateForm ">//GEN-BEGIN:|114-getter|0|114-preInit
    /**
     * Returns an initiliazed instance of updateForm component.
     * @return the initialized component instance
     */
    public WaitScreen getUpdateForm() {
        if (updateForm == null) {//GEN-END:|114-getter|0|114-preInit
            // write pre-init user code here
            updateForm = new WaitScreen(getDisplay());//GEN-BEGIN:|114-getter|1|114-postInit
            updateForm.setTitle("Update Form");
            updateForm.setCommandListener(this);
            updateForm.setFullScreenMode(true);
            updateForm.setText("Updating form...");
            updateForm.setTask(getTask());//GEN-END:|114-getter|1|114-postInit
            // write post-init user code here
        }//GEN-BEGIN:|114-getter|2|
        return updateForm;
    }
    //</editor-fold>//GEN-END:|114-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|119-getter|0|119-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|119-getter|0|119-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|119-getter|1|119-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|119-getter|1|119-execute
                    // write task-execution user code here
                    updateForm();
                }//GEN-BEGIN:|119-getter|2|119-postInit
            });//GEN-END:|119-getter|2|119-postInit
            // write post-init user code here
        }//GEN-BEGIN:|119-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|119-getter|3|




    //</editor-fold>

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(midlet);
    }

    /**
     * Exits MIDlet.
     * Note you have to implement proper MIDlet destroying.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        // midlet.destroyApp(true);
        midlet.notifyDestroyed();
    }

    public void saveConfiguration(){
        String serverURL = getInputServerURL().getString();
        String username = getInputUsername().getString();
        String password = getInputPassword().getString();
        PersistenceStoreProvider.saveServerConfiguration(serverURL, username, password);
        showAlert(configSaved, getDisplay().getCurrent());
    }
    public void loadConfiguration(){
        ServerConfigData cd = PersistenceStoreProvider.getServerConfiguration();
        getInputServerURL().setString(cd.getServerAddress() == null ? "" : cd.getServerAddress());
        getInputUsername().setString(cd.getUsername() == null ? "" : cd.getUsername());
        getInputPassword().setString(cd.getPassword() == null ? "" : cd.getPassword());
    }

    public void updateForm(){
        try{
            String formContent = WebServiceProvider.callGetCurrentRegistrationFormWS();
            // first check whether the received form is valid
            try{
                ByteArrayInputStream bais = new ByteArrayInputStream(formContent.getBytes("UTF-8"));
                FormDef formDef = XFormUtils.getFormFromInputStream(bais);
                this.midlet.getFormController().loadUpdatedForm(formDef);
                PersistenceStoreProvider.updateFormContent(formContent);
                showAlert(formUpdatedAlert, regFormSettingsForm);
            }catch(Exception e){
                // show error that form is invalid
                showAlert(couldNotUpdateAlert, regFormSettingsForm);
            }
        }catch(TechnicalException te){
            showAlert(couldNotUpdateAlert, regFormSettingsForm);
        }
    }

    public void saveFormLanguage(){
        int selectedLanguageIndex = this.choiceGroup.getSelectedIndex();
        String selectedLanguage = this.choiceGroup.getString(selectedLanguageIndex);
        getLanguageItem().setText(selectedLanguage);
        PersistenceStoreProvider.saveFormLanguage(selectedLanguage);
        if(this.languageSelectorScreen){
            switchToPreviousDisplayable();
        }else{
            this.midlet.backFromLanguageSettings();
        }
    }
    public void showAlert(Alert a, Displayable current){
        switchDisplayable(a, current);
    }

    public void languageSelectorScreen(boolean settingsScreen){
        this.languageSelectorScreen = settingsScreen;
    }

    private void addAvailableLanguages(){
        selectLanguage = new Form("Select Language", new Item[] { });
        selectLanguage.addCommand(getBackCommand1());
        selectLanguage.addCommand(getOkCommand());
        selectLanguage.setCommandListener(this);
        FormController control = this.midlet.getFormController();
        String[] languages = control.getAvailableLanguages();
        choiceGroup = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
        if(languages != null){
            for(int i = 0; i< languages.length; i++){
                choiceGroup.append(languages[i], null);
            }
        }else{
        }
        selectLanguage.append(choiceGroup);
    }
}
